package com.wing.cabinet.controller.app;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.entity.CellInventoryInout;
import com.wing.cabinet.model.request.CellInventoryInoutForm;
import com.wing.cabinet.model.response.CellInventoryInoutVO;
import com.wing.cabinet.service.CellInventoryInoutService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 */
@Validated
@Api(value = "App-售刀柜上货(上货主表无展示)/领取管理", tags = "售刀柜上货(上货主表无展示)/领取管理")
@RestController("appCellInventoryInoutController")
@RequestMapping("cabinet/app/cellInventoryInout")
public class CellInventoryInoutController {

    @Autowired
    private CellInventoryInoutService cellInventoryInoutService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<CellInventoryInoutVO> list(
            @RequestParam(name = "page") Integer page
            ,@RequestParam(name = "limit") Integer limit
            ,@RequestParam(name = "sort") String sort
            ,@RequestParam(name = "order") String order
    ) {
        Page<CellInventoryInoutVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<CellInventoryInoutVO> list = cellInventoryInoutService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "领新记录", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "存刀任务编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listReceiveOrder")
    public PageResult<CellInventoryInoutVO> listReceiveOrder(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order
            , String code
            , String cabCode
            , String skuCode
            , String skuCode4Detail
            , String startTime
            , String endTime
            , String deviceCode
            , String oldSpecCode
            , String mainCabCode
            , Cabinet.CabinetNature cabinetNature
            ,Long departmentId
            ,Long creatorId
            ,Long brandId
            ,Long categoryId
            ,Long productId
            ,Long roleId
            ,String skuCode4Query
            ,String deviceCode4Query
            ,Long packId
            ,String workOrderCode
            ,CellInventoryInout.BusinessType businessType
            ,String skuName
            ,String creatorName
            ,Long productTypeId
            ,Long supplierId
            , String  query
    ){

        Page<CellInventoryInout> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("code",code);
        queryParam.put("cabCode",cabCode);
        queryParam.put("skuCode",skuCode);
//        queryParam.put("type",CellInventoryInout.Type.out.getValue());
        //queryParam.put("types",new Integer[]{CellInventoryInout.Type.out.getValue(),CellInventoryInout.Type.outByCabSpace.getValue()});
        queryParam.put("exportType",1);
        queryParam.put("skuCode4Detail",skuCode4Detail);
        queryParam.put("startCreateTime",startTime);
        queryParam.put("endCreateTime",endTime);
        queryParam.put("deviceCode",deviceCode);
        queryParam.put("oldSpecCode",oldSpecCode);
        queryParam.put("mainCabCode",mainCabCode);
        queryParam.put("skuName",skuName);
        if (cabinetNature!=null){
            queryParam.put("cabinetNature",cabinetNature.getValue());
        }
        if (businessType!=null){
            queryParam.put("businessType",businessType.getValue());
        }
        if (StringUtils.isNotBlank(query)){
            queryParam.put("query",query);
        }
        queryParam.put("enterpriseCode", JwtUtils.getEnterpriseCode());
        queryParam.put("departmentId",departmentId);
        queryParam.put("creator",creatorId);
        queryParam.put("brandId",brandId);
        queryParam.put("categoryId",categoryId);
        queryParam.put("productId",productId);
        queryParam.put("roleId",roleId);
        queryParam.put("oldSpecCode",oldSpecCode);
        queryParam.put("skuCode4Query",skuCode4Query);
        queryParam.put("deviceCode4Query",deviceCode4Query);
        queryParam.put("packId",packId);
        queryParam.put("workOrderCode",workOrderCode);

        queryParam.put("creatorName",creatorName);
        queryParam.put("productTypeId",productTypeId);
        queryParam.put("supplierId",supplierId);
        IPage<CellInventoryInoutVO> list = cellInventoryInoutService.pageList(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long cellInventoryInoutId) {
        CellInventoryInoutVO cellInventoryInoutVO = cellInventoryInoutService.get4appById(cellInventoryInoutId);
        if (cellInventoryInoutVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), cellInventoryInoutVO);
    }

    @PostMapping()
    public JsonResult add(@Validated CellInventoryInoutForm cellInventoryInoutForm) {
        if (cellInventoryInoutService.add(cellInventoryInoutForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }


    /**
     * app 统计订单
     * PickOrder 领用订单
     * RepairOrder 维修订单
     * RecycleOrder 回收订单
     */
    @GetMapping("/getOrderNum")
    public JsonResult getOrder() {

        Map<String,Integer>counMap = cellInventoryInoutService.getCount();
        return JsonResult.success(counMap);
    }
}

